#![allow(unused_variables)]
use sqlx::Error;
use super::CountNum;
use super::OrderItemBean;
use super::OrderItemQueryRequest;
use super::Table;

impl<'c> Table<'c, OrderItemBean> {
pub async fn activate_order_item(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_order_items set active = true where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn add_order_item(&self,order_item:OrderItemBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"insert into cs_order_items ( name,active,price,amount,goods_id,cust_order_id) values (?,?,?,?,?,?)"#
		)
		.bind(order_item.name)
		.bind(order_item.active)
		.bind(order_item.price)
		.bind(order_item.amount)
		.bind(order_item.goods_id)
		.bind(order_item.cust_order_id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn count_active_order_item_records(&self) ->Result<CountNum, Error> {
		let result = sqlx::query_as(
		r#"select count(*) as countNum from cs_order_items where active = true"#
		)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn count_search_order_items_by_fields_records(&self,order_item_query_request:OrderItemQueryRequest) ->Result<CountNum, Error> {
		let mut query = r#"select count(*) as countNum from cs_order_items where 1=1 "#.to_string();
		let name = order_item_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = order_item_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let price = order_item_query_request.price.unwrap_or_default();
		if !price.is_empty() {
			let query_price  = format!("{}{}{}"," and price = ",price," ");
			query = format!("{} {}", query, query_price);
		}
		let amount = order_item_query_request.amount.unwrap_or_default();
		if !amount.is_empty() {
			let query_amount  = format!("{}{}{}"," and amount = ",amount," ");
			query = format!("{} {}", query, query_amount);
		}
		let goods_id = order_item_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let cust_order_id = order_item_query_request.cust_order_id.unwrap_or_default();
		if !cust_order_id.is_empty() {
			let query_cust_order_id  = format!("{}{}{}"," and cust_order_id = ",cust_order_id," ");
			query = format!("{} {}", query, query_cust_order_id);
		}
		let result = sqlx::query_as(&query)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn delete_order_item(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"delete from cs_order_items where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_order_item_by_id(&self,id:i64) ->Result<OrderItemBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where id = ? "#
		)
		.bind(id)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn find_order_item_by_name(&self,name:String) ->Result<OrderItemBean, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where name = ?;"#
		)
		.bind(name)
		.fetch_one(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_active_order_items(&self) ->Result<Vec<OrderItemBean>, Error> {
		let result = sqlx::query_as(
		  r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where active = true order by name asc"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn list_all_order_items(&self) ->Result<Vec<OrderItemBean>, Error> {
		let result = sqlx::query_as(
	r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items"#
		)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_order_items_by_fields(&self,order_item_query_request:OrderItemQueryRequest) ->Result<Vec<OrderItemBean>, Error> {
		let mut query = r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where 1=1 "#.to_string();
		let name = order_item_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = order_item_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let price = order_item_query_request.price.unwrap_or_default();
		if !price.is_empty() {
			let query_price  = format!("{}{}{}"," and price = ",price," ");
			query = format!("{} {}", query, query_price);
		}
		let amount = order_item_query_request.amount.unwrap_or_default();
		if !amount.is_empty() {
			let query_amount  = format!("{}{}{}"," and amount = ",amount," ");
			query = format!("{} {}", query, query_amount);
		}
		let goods_id = order_item_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let cust_order_id = order_item_query_request.cust_order_id.unwrap_or_default();
		if !cust_order_id.is_empty() {
			let query_cust_order_id  = format!("{}{}{}"," and cust_order_id = ",cust_order_id," ");
			query = format!("{} {}", query, query_cust_order_id);
		}
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn search_order_items_by_fields_by_page(&self,order_item_query_request:OrderItemQueryRequest) ->Result<Vec<OrderItemBean>, Error> {
		let mut query = r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where 1=1 "#.to_string();
		let name = order_item_query_request.name.unwrap_or_default();
		if !name.is_empty() {
			let query_name  = format!("{}{}{}"," and name like '%",name,"%' ");
			query = format!("{} {}", query, query_name);
		}
		let active = order_item_query_request.active.unwrap_or_default();
		if !active.is_empty() && (active=="true" || active =="false"){
			let query_active  = format!("{}{}{}"," and active = ",active," ");
			query = format!("{} {}", query, query_active);
		}
		let price = order_item_query_request.price.unwrap_or_default();
		if !price.is_empty() {
			let query_price  = format!("{}{}{}"," and price = ",price," ");
			query = format!("{} {}", query, query_price);
		}
		let amount = order_item_query_request.amount.unwrap_or_default();
		if !amount.is_empty() {
			let query_amount  = format!("{}{}{}"," and amount = ",amount," ");
			query = format!("{} {}", query, query_amount);
		}
		let goods_id = order_item_query_request.goods_id.unwrap_or_default();
		if !goods_id.is_empty() {
			let query_goods_id  = format!("{}{}{}"," and goods_id = ",goods_id," ");
			query = format!("{} {}", query, query_goods_id);
		}
		let cust_order_id = order_item_query_request.cust_order_id.unwrap_or_default();
		if !cust_order_id.is_empty() {
			let query_cust_order_id  = format!("{}{}{}"," and cust_order_id = ",cust_order_id," ");
			query = format!("{} {}", query, query_cust_order_id);
		}
		let pagenum =  order_item_query_request.page.unwrap_or_default();
		let pagesize =  order_item_query_request.rows.unwrap_or_default();
		let start = (pagenum - 1) * pagesize;
		let limit = pagesize;
		query = format!("{} limit {} offset {} ", query, limit, start);
		let result = sqlx::query_as(&query)
		.fetch_all(&(&*self.pool).clone().unwrap())
		.await;
		return result;
}

pub async fn soft_delete_order_item(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_order_items set active = false where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn toggle_order_item(&self,id:i64) ->Result<u64, Error> {
		let result = sqlx::query(
	r#"update cs_order_items set active = not active where id = ? "#
		)
		.bind(id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn update_order_item(&self,order_item:OrderItemBean) ->Result<u64, Error> {
		let result = sqlx::query(
		r#"update cs_order_items set name = ? ,active = ? ,price = ? ,amount = ? ,goods_id = ? ,cust_order_id = ? where id = ? "#
		)
		.bind(order_item.name)
		.bind(order_item.active)
		.bind(order_item.price)
		.bind(order_item.amount)
		.bind(order_item.goods_id)
		.bind(order_item.cust_order_id)
		.bind(order_item.id)
		.execute(&(&*self.pool).clone().unwrap())
		.await;
		match result {
			Ok(result) => {
				Ok(result.rows_affected())
			},
			Err(err) => {
				Err(err)
			}
		}
}

pub async fn find_order_item_indexed_name(&self,short_name:String) ->Result<Vec<OrderItemBean>, Error> {
	let result = sqlx::query_as(
	r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where name like concat(? ,'%') "#
	)
	.bind(short_name)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
		return result;
}

pub async fn list_my_order_items(&self,cust_order_id:i64) ->Result<Vec<OrderItemBean>, Error> {
    let result = sqlx::query_as(
	  r#"select id,name,active,price,amount,goods_id,cust_order_id from cs_order_items where cust_order_id = ? and active = true order by name asc"#
	)
    .bind(cust_order_id)
	.fetch_all(&(&*self.pool).clone().unwrap())
	.await;
	return result;
}

}
